package org.lyd.spring.aop.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @Author:lyd
 *验证有多重切面执行顺序如何
 **/
@Order(100)
@Component
@Aspect
public class AuthAspect {
    @Pointcut("execution(int org.lyd.spring.aop.calculator.Calculator.*(..))")
    public void pointCut() {
    }

    @Around("pointCut()")
    //环绕通知示例
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("【Auth】-->开始执行");
        Object result = null;
        try {
            result = joinPoint.proceed();
            System.out.println("【Auth】-->返回结果为：" + result);
        } catch (Throwable t) {
            System.out.println("【Auth】-->异常为：" + t.getMessage());
            //环绕通知遇到异常时需抛出，否则外部的切面无法处理到异常
            throw  t;
        }finally {
            System.out.println("【Auth】-->执行结束");
        }
        return result;
    }


//
//    @Before("pointCut()")
//    public void before() {
//        System.out.println("【Auth】-->开始执行");
//    }
//    @AfterReturning(value = "pointCut()",returning = "result")
//    public void afterReturning(Object result) {
//        System.out.println("【Auth】-->返回结果为：" + result);
//    }
//    @After("pointCut()")
//    public void after() {
//        System.out.println("【Auth】-->执行结束");
//    }
//
//    @AfterThrowing(value = "pointCut()",throwing = "exception")
//    public void afterThrowing(Exception exception) {
//        System.out.println("【Auth】-->异常为：" + exception.getMessage());
//    }

}
